var express = require('express')
var router = express.Router()

const { getALL, exec, getById } = require('../db')
const { createToken } = require('../utils/user-jwt')

//post/login,登录页面接口,
router.post('/', async (req, res) => {
  //解析请求参数
  const { username, password } = req.body
  //操作数据库,
  let sql = `select * from gg_user where username='${username}' `

  let data = await getALL(sql)
  //1. 根据用户名判断用户是否存在
  if (!data[0]) {
    //如果用户不存在,返回状态
    return res.send({
      code: 422,
      messages: '用户名不存在!',
    })
  } else if (data[0].password !== password) {
    //如果密码不相等,返回状态
    return res.send({
      code: 423,
      messages: '密码输入错误!',
    })
  } else if (data[0].user_other == 0) {
    return res.send({
      code: 424,
      messages: '该用户已删除,请联系管理员',
    })
  } else if (data[0].user_type === 'admin') {
    //生成token,sgin(参数1,传递描述的信息;参数2,秘钥密码;expiresIn:时效)第一个参数必须是对象
    const token = createToken(data[0].id)
    //登录成功返回token给前端
    res.send({
      code: 201,
      data: data[0],
      messages: '欢迎你,超级管理员!',
      token: token,
      options: {
        public: true,
        common: true,
        traffic: true,
        manage: true,
      },
    })
  } else if (data[0].user_type === 'traffic') {
    const id = data[0].user_id
    // 更新状态
    sql = `update gg_user set user_onlinestatus = 1 where user_id = ${id}`
    await exec(sql)
    sql = `select * from gg_user where username='${username}' `
    data = await getALL(sql)
    // 返回数据
    const token = createToken(data[0].id)
    res.send({
      code: 201,
      data: data[0],
      messages: '欢迎你,交管部门!',
      token: token,
      options: {
        public: true,
        common: false,
        traffic: true,
        manage: false,
      },
    })
  } else {
    const id = data[0].user_id
    sql = `update gg_user set user_onlinestatus = 1 where user_id = ${id}`
    await exec(sql)
    sql = `select * from gg_user where username='${username}' `
    data = await getALL(sql)
    const token = createToken(data[0].id)
    res.send({
      code: 201,
      data: data[0],
      messages: '欢迎你,普通用户!',
      token: token,
      options: {
        public: true,
        common: true,
        traffic: false,
        manage: false,
      },
    })
  }
})

// 调用路由前是否被强制下线
router.get('/:id', async (req, res) => {
  const { id } = req.params
  let sql = `select * from gg_user where user_id='${id}' `
  const data = await getById(sql)
  if (data.user_onlinestatus == 0) {
    res.send({
      code: 404,
      messages: '该用户已被强制下线',
    })
  } else {
    res.send({
      code: 203,
      messages: '正常使用',
    })
  }
})

router.put('/:id', async (req, res) => {
  const { id } = req.params
  let sql = `update gg_user set user_onlinestatus = '0' where user_id = ${id}`
  await exec(sql)
  res.send({
    code: 202,
    messages: '登出成功',
  })
})

// 修改密码
router.post('/password', async (req, res) => {
  //解析请求参数
  const { username, password, newpassword } = req.body
  console.log(username, password, newpassword)
  let sql = `select * from gg_user where username='${username}' `
  console.log(sql)
  const data = await getById(sql)
  console.log(data)
  if (data.password != password) {
    res.send({
      code: 400,
      messages: '原密码错误',
    })
  } else {
    sql = `update gg_user set password = ${newpassword} where username='${username}'`
    await exec(sql)
    res.send({
      code: 204,
      messages: '密码修改成功',
    })
  }
})
module.exports = router
